<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class charge extends CI_Controller {

	public function index()
	{
		$this->load->view('v_charge');
	}
	
	//Fuction that recieves a request
	public getRequest()
	{	
		//Expect to be sent in a request:
		//Vendor ID
		//Payment amount
		//TransactionID
		//Credit Card Number
		//Experation Date
		//Card Holder
		//Card Type
		//Security Code
		
		$vendorid 		= $this->input->post('vendorid', TRUE);
		$payamount		= $this->input->post('payamount', TRUE);
		$transactionid	= $this->input->post('transactionid', TRUE);
		$creditcardnum	= $this->input->post('creditcardnum', TRUE);
		$expdate		= $this->input->post('expdate', TRUE);
		$cardholder		= $this->input->post('cardholder', TRUE);
		$cardtype		= $this->input->post('cardtype', TRUE);
		$securitycode	= $this->input->post('securitycode', TRUE);
		
		$requestResponse;
		$requestResponse->transactionid = $transactionid;		
		$requestResponse->decryption = "yes";			

		
		//If any of the charge request fields is empty or NULL
		if(($vendorid == '' || $vendorid ==  NULL)||
		   ($payamount == '' || $payamount == NULL)||
		   ($transactionid == '' || $transactionid == NULL)||
		   ($creditcardnum == '' || $creditcardnum == NULL)||
		   ($expdate == '' || $expdate == NULL)||
		   ($cardholder == '' || $cardholder == NULL)||
		   ($cardtype == '' || $cardtype == NULL)||
		   ($securitycode == '' || $securitycode == NULL))
		{
			$requestResponse->status = "Error";
			$requestResponse->code = 1;
			$requestResponse->description = "One or more empty fields";
		}else 
		//If payment is less than $0.00
		if($payamount <= 0.00)
		{
			$requestResponse->status = "Error";
			$requestResponse->code = 2;
			$requestResponse->description = "Invalid payment amount";
		}else 
		//If experation date has already passed
		if($expdate <= time())
		{
			$requestResponse->status = "Error";
			$requestResponse->code = 3;
			$requestResponse->description = "Expired card";
		}else 
		//If credit card number is outside of 0000-0000-0000-0000 thru 9999-9999-9999-9999
		if($creditcardnum < 0 || $creditcardnum > 9999999999999999)
		{
			$requestResponse->status = "Error";
			$requestResponse->code = 4;
			$requestResponse->description = "Invalid credit card number";
		}
		//If no errors got caught, valid request was received.
		else
		{
			$requestResponse->status = "Success";
			$requestResponse->code = 0;
			$requestResponse->description = "Valid request received";
		}
		
		header('Content-type: application/json');
		echo json_encode($requestResponse);
	}
	
	public function processPayments()
	{
		//Will process payments. Hard coded to check for some errors.
	
	
		//$url = site_url("charge/getRequest");
		//$json = NULL;
		
		$userid 		= $this->input->post('userid', TRUE);
		$vendorid 		= $this->input->post('vendorid', TRUE);
		$payamount		= $this->input->post('payamount', TRUE);
		$transactionid	= $this->input->post('transactionid', TRUE);
		$creditcardnum	= $this->input->post('creditcardnum', TRUE);
		$expdate		= $this->input->post('expdate', TRUE);
		$cardholder		= $this->input->post('cardholder', TRUE);
		$cardtype		= $this->input->post('cardtype', TRUE);
		$securitycode	= $this->input->post('securitycode', TRUE;	
		
		$paymentResponse;
		$paymentResponse->transactionid = $transactionid;
		$requestResponse->decryption = "yes";			

		//Verify user
		if($userid != "Bob Jones")
		{
			$paymentResponse->status = "Error";
			$requestResponse->code = 1;
			$requestResponse->description = "Unable to locate card holder";
		}else 
		//Verify vendor
		if($vendorid != "Verizon" || $vendorid != "Best Buy" || 
				 $vendorid != "Amazon" || $vendorid != "CSULB" || $vendorid != "Time Warner Cable")
		{
			$paymentResponse->status = "Error";
			$requestResponse->code = 2;
			$requestResponse->description = "Unable to locate vendor";
		}else 
		//Verify creditcard number
		if($creditcardnum != 1234123412341234 || 
				 $creditcardnum != 1111111111111111 ||
				 $creditcardnum != 9999999999999999)
		{
			$paymentResponse->status = "Error";
			$requestResponse->code = 3;
			$requestResponse->description = "Unable to locate credit card number";		
		}else 
		//Verify matching card number and card holder
		if(($creditcardnum != 1234123412341234|| 
				 $creditcardnum != 1111111111111111 ||
				 $creditcardnum != 9999999999999999)) && $userid != "Bob Jones")
		{
			$paymentResponse->status = "Error";
			$requestResponse->code = 4;
			$requestResponse->description = "Card holder and number do not match";		
		}
		//If no errors got caught, payment was processed
		else
		{
			$paymentResponse->status = "Success";
			$requestResponse->code = 0;
			$requestResponse->description = "Payment processed";
			//Update financial database

		}
		
		header('Content-type: application/json');
		echo json_encode($requestResponse);
	}
}
?>